<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>简单测试</title>
</head>
<body>
    <h1>简单数据库测试</h1>
    
    <?php
    error_reporting(E_ALL);
    ini_set('display_errors', 1);
    
    $host = '113.44.136.17';
    $port = 3306;
    $database = 'qiandao';
    $username = 'qiandao';
    $password = 'NGbks3HWx5PhSxDX';
    
    try {
        $pdo = new PDO("mysql:host={$host};port={$port};dbname={$database};charset=utf8mb4", $username, $password);
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        
        echo "<p>✓ 数据库连接成功</p>";
        
        echo "<h2>一级菜单列表</h2>";
        
        $stmt = $pdo->query("
            SELECT id, title, sort, status, url, icon,
                   (SELECT COUNT(*) FROM system_menu WHERE pid = m.id AND status = 1) as sub_count
            FROM system_menu m
            WHERE pid = 0
            ORDER BY sort DESC, id ASC
        ");
        
        $menus = $stmt->fetchAll(PDO::FETCH_ASSOC);
        
        echo "<table border='1' cellpadding='5' cellspacing='0'>";
        echo "<tr><th>ID</th><th>标题</th><th>SORT</th><th>状态</th><th>子菜单数</th><th>URL</th><th>图标</th><th>会显示?</th></tr>";
        
        foreach ($menus as $menu) {
            $status = $menu['status'] == 1 ? '启用' : '禁用';
            $willShow = ($menu['status'] == 1 && $menu['sub_count'] > 0) ? '✓ 是' : '❌ 否';
            $color = ($menu['status'] == 1 && $menu['sub_count'] > 0) ? 'green' : 'red';
            
            echo "<tr>";
            echo "<td>{$menu['id']}</td>";
            echo "<td><strong>{$menu['title']}</strong></td>";
            echo "<td>{$menu['sort']}</td>";
            echo "<td>{$status}</td>";
            echo "<td>{$menu['sub_count']}</td>";
            echo "<td>{$menu['url']}</td>";
            echo "<td>" . ($menu['icon'] ?: '(无)') . "</td>";
            echo "<td style='color:{$color}'>{$willShow}</td>";
            echo "</tr>";
        }
        
        echo "</table>";
        
        echo "<h2>系统管理的子菜单</h2>";
        
        $stmt = $pdo->query("
            SELECT id, pid, title, status, url, node, sort
            FROM system_menu
            WHERE id = 1 OR pid = 1 OR pid IN (SELECT id FROM system_menu WHERE pid = 1)
            ORDER BY 
                CASE WHEN id = 1 THEN 0 WHEN pid = 1 THEN 1 ELSE 2 END,
                sort DESC, id ASC
        ");
        
        $sysMenus = $stmt->fetchAll(PDO::FETCH_ASSOC);
        
        echo "<pre>";
        foreach ($sysMenus as $menu) {
            $indent = '';
            if ($menu['pid'] == 1) {
                $indent = '  ├─ ';
            } elseif ($menu['pid'] > 1) {
                $indent = '  │  └─ ';
            }
            
            $status = $menu['status'] == 1 ? '启用' : '禁用';
            echo "{$indent}{$menu['title']} (ID:{$menu['id']}, {$status}, SORT:{$menu['sort']}, URL:{$menu['url']})\n";
        }
        echo "</pre>";
        
        echo "<h2>结论</h2>";
        
        $sysMenuData = null;
        foreach ($menus as $m) {
            if ($m['id'] == 1) {
                $sysMenuData = $m;
                break;
            }
        }
        
        if ($sysMenuData) {
            if ($sysMenuData['status'] == 0) {
                echo "<p style='color:red'>❌ 系统管理菜单被禁用了</p>";
            } elseif ($sysMenuData['sub_count'] == 0) {
                echo "<p style='color:red'>❌ 系统管理没有启用的子菜单</p>";
            } else {
                echo "<p style='color:green'>✓ 数据库配置正常，系统管理应该能显示</p>";
                echo "<p>如果后台看不到，问题在于：</p>";
                echo "<ul>";
                echo "<li>权限过滤（子菜单的 node 权限检查失败）</li>";
                echo "<li>浏览器缓存</li>";
                echo "<li>Session 缓存</li>";
                echo "</ul>";
            }
        }
        
    } catch (PDOException $e) {
        echo "<p style='color:red'>数据库错误: " . $e->getMessage() . "</p>";
    }
    ?>
    
</body>
</html>

